package com.dlwx.firefighting.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.activity.AfterSaleServiceActivity;
import com.dlwx.firefighting.activity.BalanceActivity;
import com.dlwx.firefighting.activity.LoginActivity;
import com.dlwx.firefighting.activity.TuiGuangActivity;
import com.dlwx.firefighting.activity.UserCollectActivity;
import com.dlwx.firefighting.activity.UserIntegralActivity;
import com.dlwx.firefighting.activity.UserSettingActivity;
import com.dlwx.firefighting.bean.UserBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.ActivityCollector;
import com.dlwx.firefighting.utils.CacheManager;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.PhoneInfoUtil;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.dlwx.firefighting.widget.CircleImageView;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.listener.OnOperItemClickL;
import com.flyco.dialog.widget.ActionSheetDialog;
import com.flyco.dialog.widget.NormalDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.jpush.android.api.JPushInterface;

/**
 * 我的界面
 */
public class MyFragment extends Fragment {


    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.title_left_one_tv)
    TextView titleLeftOneTv;
    @BindView(R.id.title_left_two_btn)
    ImageView titleLeftTwoBtn;
    @BindView(R.id.title_left_two_tv)
    TextView titleLeftTwoTv;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.title_right_one_btn)
    ImageView titleRightOneBtn;
    @BindView(R.id.title_right_two_btn)
    ImageView titleRightTwoBtn;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.title_right_one_tv2)
    TextView titleRightOneTv2;
    @BindView(R.id.img_header)
    CircleImageView imgHeader;
    @BindView(R.id.tv_denglu_zhuce)
    TextView tvDengluZhuce;
    @BindView(R.id.tv_username)
    TextView tvUsername;
    @BindView(R.id.user_level)
    TextView userLevel;
    @BindView(R.id.tv_my_integral)
    TextView tvMyIntegral;
    @BindView(R.id.tv_my_collect)
    TextView tvMyCollect;
    @BindView(R.id.tv_my_tuiguang)
    TextView tvMyTuiguang;
    @BindView(R.id.ll_red_package)
    RelativeLayout llRedPackage;
    @BindView(R.id.rl_datalist)
    RelativeLayout rlDatalist;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.rl_wallet)
    RelativeLayout rlWallet;
    @BindView(R.id.tv_integral)
    TextView tvIntegral;
    @BindView(R.id.rl_integral)
    RelativeLayout rlIntegral;
    @BindView(R.id.rl_collect)
    RelativeLayout rlCollect;
    @BindView(R.id.rl_feedback)
    RelativeLayout rlFeedback;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.rl_about_us)
    RelativeLayout rlAboutUs;
    @BindView(R.id.tv_clear_up)
    TextView tvClearUp;
    @BindView(R.id.rl_clear_up)
    RelativeLayout rlClearUp;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.rl_sys_update)
    RelativeLayout rlSysUpdate;
    @BindView(R.id.rl_tuiguang)
    RelativeLayout rlTuiguang;
    Unbinder unbinder;
    @BindView(R.id.tv_loginout)
    TextView tvLoginout;
    private String size;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();

        getDatas();
    }

    private void getDatas() {
//        OkGo.<String>post(Constant.USER)
//                .tag(this)
//                .params("user_id", userId)
//                .params("action", "user_info")
//                .execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(Response<String> response) {
//                        DealResult dealResult = new DealResult();
//                        List<UserBean> beans = dealResult.deal(getActivity(), response, new TypeToken<BaseBean<UserBean>>() {
//                        }.getType());
//                        setDatas(beans);
//                    }
//                    @Override
//                    public void onError(Response<String> response) {
//                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
//                        super.onError(response);
//                    }
//                });
    }

    private void setDatas(List<UserBean> beans) {

    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText(getResources().getString(R.string.my));
        titleRightOneBtn.setVisibility(View.VISIBLE);
        titleRightOneBtn.setImageResource(R.mipmap.icon_setting);
        //如果已经登录则显示昵称和等级，否则显示登录按钮
       /* boolean login = (boolean) SPUtils.get(getActivity(), Constant.isLogin, false);
        if (login) {
            tvUsername.setVisibility(View.VISIBLE);
            tvLevel.setVisibility(View.VISIBLE);
            tvDengluZhuce.setVisibility(View.GONE);
        }*/

        //设置当前版本号
        String version = PhoneInfoUtil.getInstance().getVersion(getActivity());
        if (TextUtils.isEmpty(version)) {
            tvVersion.setText("");
        } else {
            tvVersion.setText("版本："+version);
        }

        //查看缓存大小
        try {
            size = CacheManager.getTotalCacheSize(getActivity().getApplicationContext());
            if (size != null) {
                tvClearUp.setText(size);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.ll_red_package, R.id.rl_datalist, R.id.rl_wallet, R.id.rl_integral, R.id.rl_collect, R.id.rl_feedback
            , R.id.rl_about_us, R.id.rl_clear_up, R.id.rl_sys_update, R.id.rl_tuiguang, R.id.title_right_one_btn,R.id.tv_loginout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_right_one_btn:
                startActivity(new Intent(getActivity(), UserSettingActivity.class));
                break;
            case R.id.ll_red_package:
                break;
            case R.id.rl_datalist:
                break;
            case R.id.rl_wallet:
                startActivity(new Intent(getActivity(), BalanceActivity.class));
                break;
            case R.id.rl_integral:
                startActivity(new Intent(getActivity(), UserIntegralActivity.class));
                break;
            case R.id.rl_collect:
                startActivity(new Intent(getActivity(), UserCollectActivity.class));
                break;
            case R.id.rl_feedback:
                startActivity(new Intent(getActivity(), AfterSaleServiceActivity.class));
                break;
            case R.id.rl_about_us:
                break;
            case R.id.rl_clear_up:
                String[] stringItems = {"清除缓存"};
                final ActionSheetDialog sheetDialog = DialogUtils.getBottomSheetDialog(getActivity(),
                        "确定要清除缓存数据吗？", stringItems);
                sheetDialog.setOnOperItemClickL(new OnOperItemClickL() {
                    @Override
                    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (position == 0) {
                            CacheManager.clearAllCache(getActivity().getApplicationContext());
                            ToastUtils.toastForShort(getActivity(), "清除缓存成功");
                            tvClearUp.setText("0M");
                        }
                        sheetDialog.dismiss();
                    }
                });
                break;
            case R.id.rl_sys_update:
                break;
            case R.id.rl_tuiguang:
                startActivity(new Intent(getActivity(), TuiGuangActivity.class));
                break;
            case R.id.tv_loginout:
                logOut();
                break;
        }
    }

    private boolean isRemember;
    private String name;
    private String psw;
    NormalDialog dialog;

    /**
     * 退出登录
     */
    private void logOut() {
        dialog = DialogUtils.getNormalDialog(getActivity(), "提示", "是否退出登录?");
        dialog.setOnBtnClickL(new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
            }
        }, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                dialog.dismiss();
                isRemember = (boolean) SPUtils.get(getActivity(), Constant.IS_REMEMBER, false);
                name = (String) SPUtils.get(getActivity(), Constant.LOGIN_NAME, "");
                psw = (String) SPUtils.get(getActivity(), Constant.LOGIN_PSW, "");
                SPUtils.clear(getActivity());
                SPUtils.put(getActivity(), Constant.isLogin, false);
                SPUtils.put(getActivity(), Constant.IS_REMEMBER, isRemember);
                SPUtils.put(getActivity(), Constant.LOGIN_NAME, name);
                SPUtils.put(getActivity(), Constant.LOGIN_PSW, psw);
                JPushInterface.deleteAlias(getActivity(), Constant.SUCESS);
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                ActivityCollector.getInstances().finishAllActivity();
//                    getActivity().finish();
            }
        });
    }
}
